//
//  AppDelegate.swift
//  glzbowei
//
//  Created by 高伦展 on 2017/11/16.
//  Copyright © 2017年 glz. All rights reserved.
//

import UIKit
import Alamofire

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        //AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        
        setupAppearance()
        
        window = UIWindow(frame:UIScreen.main.bounds)
        
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
   
        //监听通知
        // 参数：通知名称 | 发送通知的对象，如果为nil，监听任何对象 | nil，主线程 |
        NotificationCenter.default.addObserver(
            forName: NSNotification.Name(rawValue: WBSwitchRootViewControllerNotification),
            object: nil,
            queue: nil) { (notification) in
            
                let vc = notification.object != nil ? WelcomeViewController():MainViewController()
                
                self.window?.rootViewController = vc
                
        }
        
        SQLiteManager.shareManager
        
        QorumLogs.enabled = true
        QorumLogs.test()
        
        return true
    }
    
    ///app 进入后台
    func applicationDidEnterBackground(_ application: UIApplication) {
        StatusDAL.clearDataCache()
    }
    
    deinit {
        //注销通知
        // 参数：监听者，监听的通知，发送通知的对象
        NotificationCenter.default.removeObserver(self,
                                                  name: NSNotification.Name(rawValue: WBSwitchRootViewControllerNotification),
                                                  object: nil)
    }
    
    //设置全局外观 - 在很多应用程序中，都会在 AppDelegate 中设置所有需要控件的全局外观
    private func setupAppearance(){
        //修改导航栏的全局外观，要在控件创建之前设置，一经设置全局有效
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        
        UITabBar.appearance().tintColor = WBAppearanceTintColor
    }
 
}

// MARK: - 界面切换代码
extension AppDelegate{
    // 启动的根视图控制器
    private var defaultRootViewController:UIViewController{
        //1.判断是否登录
        if UserAccountViewModel.sharedUserAccount.userLogin {
            return isNewVersion ? NewFeatureViewController() : WelcomeViewController()
        }
        //2.没有登录返回主控制器
        return MainViewController()
    }
    
    // 判断是否新版本
    private var isNewVersion:Bool{
        
        //1.当前的版本
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        
        print(version)
        
        //2.‘之前’的版本，把当前版本保存在用户偏好
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = UserDefaults.standard.double(forKey: sandboxVersionKey)
        
        UserDefaults.standard.set(version, forKey: sandboxVersionKey)
        
        return version > sandboxVersion
    }
    
}

